package it.polimi.traveldream.ejb.entities;

import it.polimi.traveldream.ejbclient.dtos.UserDTO;
import it.polimi.traveldream.utils.enums.UserRole;

import java.io.Serializable;

import javax.persistence.*;

import org.apache.commons.codec.digest.DigestUtils;

import java.sql.Timestamp;
import java.util.Date;


/**
 * The persistent class for the user database table.
 * 
 */
@Entity
@NamedQueries({
        @NamedQuery(name=User.FIND_USERS_BY_ROLE,
                        query="SELECT u FROM User u WHERE u.role = :role ORDER BY u.registration ASC"),
        @NamedQuery(name=User.FIND_USER_BY_EMAIL,
                        query="SELECT u FROM User u WHERE u.email = :email")
})
public class User implements Serializable {
	private static final long serialVersionUID = -5138370671736823507L;
    public static final String FIND_USERS_BY_ROLE = "User.findUsersByRole";
    public static final String FIND_USER_BY_EMAIL = "User.findUserByEmail";
    private int id;
    private String email;
    private String name;
    private String password;
    private Timestamp registration;
    private UserRole role;
    private String surname;

    public User() {
            super();
    }

    public User(UserDTO user) {
    	super();
        this.id = user.getId();
        this.email = user.getEmail();
        this.name = user.getName();
        this.password = DigestUtils.sha256Hex(user.getPassword());
        this.registration = new Timestamp(new Date().getTime());
        this.role = user.getRole();
        this.surname = user.getSurname();
    }
    
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public int getId() {
            return this.id;
    }

    public void setId(int id) {
            this.id = id;
    }


    public String getEmail() {
            return this.email;
    }

    public void setEmail(String email) {
            this.email = email;
    }


    public String getName() {
            return this.name;
    }

    public void setName(String name) {
            this.name = name;
    }


    public String getPassword() {
            return this.password;
    }

    public void setPassword(String password) {
            this.password = password;
    }

    public Timestamp getRegistration() {
            return this.registration;
    }

    public void setRegistration(Timestamp registration) {
            this.registration = registration;
    }

    @Enumerated(EnumType.STRING)
    public UserRole getRole() {
            return this.role;
    }

    public void setRole(UserRole role) {
            this.role = role;
    }


    public String getSurname() {
            return this.surname;
    }

    public void setSurname(String surname) {
            this.surname = surname;
    }
    
}